package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.domain.condition.executionhistory.ExecutionHistoryCondition;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.form.executionhistory.ExecutionHistoryCreateForm;
import com.bwda.dsrs.filecenter.domain.form.executionhistory.ExecutionHistoryQueryForm;
import com.bwda.dsrs.filecenter.domain.form.executionhistory.ExecutionHistoryUpdateForm;
import com.bwda.dsrs.filecenter.domain.po.executionhistory.ExecutionHistoryPo;
import com.bwda.dsrs.filecenter.domain.vo.executionhistory.ExecutionHistoryVo;
import com.bwda.dsrs.filecenter.service.ExecutionHistoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/executionhistory")
@Api(value = "/b/executionhistory", tags = "执行历史")
public class ExecutionHistoryController extends BaseController {

    @Autowired
    private ExecutionHistoryService executionHistoryService;

    @ApiOperation(value = "查询执行历史", notes = "查询执行历史", httpMethod = "GET")
    @RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<ExecutionHistoryVo> query(@ApiParam(value = "", required = true) @RequestParam Integer historyId) throws BusinessServiceException {
        ExecutionHistoryPo po = executionHistoryService.queryWithValid(historyId);
        ExecutionHistoryVo vo = CopyUtil.transfer(po, ExecutionHistoryVo.class);
        return getSuccessResult(vo);
    }

    @ApiOperation(value = "查询执行历史数量", notes = "查询执行历史数量", httpMethod = "GET")
    @RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<Integer> queryCount(@ModelAttribute @Valid ExecutionHistoryQueryForm form) throws BusinessServiceException {
        ExecutionHistoryCondition condition = this.getConditionByQueryForm(form);
        int count = executionHistoryService.queryCount(condition);
        return getSuccessResult(count);
    }

    @ApiOperation(value = "查询执行历史列表", notes = "查询执行历史列表", httpMethod = "GET")
    @RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreListResponse<ExecutionHistoryVo>> queryList(@ModelAttribute @Valid ExecutionHistoryQueryForm form) throws BusinessServiceException {
        ExecutionHistoryCondition condition = this.getConditionByQueryForm(form);
        List<ExecutionHistoryPo> poList = executionHistoryService.queryList(condition);
        List<ExecutionHistoryVo> voList = CopyUtil.transfer(poList, ExecutionHistoryVo.class);
        return getSuccessResult(getListResponse(voList));
    }

    @ApiOperation(value = "查询执行历史列表(带分页)", notes = "查询执行历史列表(带分页)", httpMethod = "GET")
    @RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<CentreCutPageResponse<ExecutionHistoryVo>> queryPageList(@ModelAttribute @Valid ExecutionHistoryQueryForm form) throws BusinessServiceException {
        ExecutionHistoryCondition condition = this.getConditionByQueryForm(form);
        condition.setTaskId(form.getExecutionPolicyId());
        List<ExecutionHistoryVo> voList = new ArrayList<>();
        int count = executionHistoryService.queryCount(condition);
        if (count > 0) {
            List<ExecutionHistoryPo> poList = executionHistoryService.queryList(condition);
            voList = CopyUtil.transfer(poList, ExecutionHistoryVo.class);
        }
        return getSuccessResult(getPageResponse(form, count, voList));
    }

    @ApiOperation(value = "新增执行历史", notes = "新增执行历史", httpMethod = "POST")
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<ExecutionHistoryVo> add(@ModelAttribute @Valid ExecutionHistoryCreateForm form) throws BusinessServiceException {
        ExecutionHistoryPo po = CopyUtil.transfer(form, ExecutionHistoryPo.class);
//        po.setHistoryId(RandomUtils.nextInt());
        executionHistoryService.insert(po);
        ExecutionHistoryVo vo = CopyUtil.transfer(po, ExecutionHistoryVo.class);
        return getSuccessResult(vo);
    }

    @ApiOperation(value = "修改执行历史", notes = "修改执行历史", httpMethod = "POST")
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity update(@ModelAttribute @Valid ExecutionHistoryUpdateForm form) throws BusinessServiceException {
        ExecutionHistoryPo po = CopyUtil.transfer(form, ExecutionHistoryPo.class);
        executionHistoryService.update(po);
        return getSuccessResult();
    }

    @ApiOperation(value = "删除执行历史", notes = "删除执行历史", httpMethod = "POST")
    @RequestMapping(value = "/deleteByIds", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity deleteByIds(@RequestParam Long[] historyIds) throws BusinessServiceException {
        executionHistoryService.deleteByIds(historyIds);
        return getSuccessResult();
    }

    /**
     * 根据id查询稽核管理历史任务
     *
     * @param auditHistoryIds 历史记录IDS
     * @return
     */
    @ApiOperation(value = "根据id查询管理历史任务", notes = "根据id查询管理历史任务", httpMethod = "POST")
    @RequestMapping(value = "/queryResAuditTaskHistoryById", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<List<ExecutionHistoryPo>> queryResAuditTaskHistoryById(@RequestParam Long[] historyIds) {

        logger.info("根据id查询稽核管理历史任务start form log auditHistoryIds:{}", historyIds);
        List<ExecutionHistoryPo> po = executionHistoryService.queryResAuditTaskHistoryById(historyIds);
        return getSuccessResult(po);
    }
    /**
     * ExecutionHistoryQueryForm转换为ExecutionHistoryCondition
     *
     * @param form
     * @return
     */
    private ExecutionHistoryCondition getConditionByQueryForm(ExecutionHistoryQueryForm form) {
        ExecutionHistoryCondition condition = CopyUtil.transfer(form, ExecutionHistoryCondition.class);
        return condition;
    }

}